//
//  getLabelCharPoint.m
//  代码布局
//
//  Created by iOS on 16/8/1.
//  Copyright © 2016年 iOS. All rights reserved.
//

#import "getLabelCharPoint.h"
#import <CoreText/CoreText.h>
@implementation getLabelCharPoint
+ (CFIndex)touchContentOffsetInView:(UIView *)view atPoint:(CGPoint)point data:(NSAttributedString *)data{
    // 创建CTFramesetterRef实例  /Users/ios/Desktop/代码布局/代码布局/getLabelCharPoint.m:14:5: Declaration of 'CTFramesetterRef' must be imported from module 'CoreText.CTFramesetter' before it is required
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)data);
    
    // 获得要缓制的区域的高度
    CGSize restrictSize = CGSizeMake(WIDTH - 16, CGFLOAT_MAX);
    CGSize coreTextSize = CTFramesetterSuggestFrameSizeWithConstraints(framesetter, CFRangeMake(0,0), nil, restrictSize, nil);
    CGFloat textHeight = coreTextSize.height;
    
    // 生成CTFrameRef实例
    CTFrameRef frame = [self createFrameWithFramesetter:framesetter config:WIDTH - 16 height:textHeight];
    CTFrameRef textFrame = frame;
    
    CFArrayRef lines = CTFrameGetLines(textFrame);
    if (!lines) {
        return -1;
    }
    CFIndex count = CFArrayGetCount(lines);
    // 获得每一行的origin坐标
    CGPoint origins[count];
    CTFrameGetLineOrigins(textFrame, CFRangeMake(0,0), origins);
    // 翻转坐标系
    CGAffineTransform transform =  CGAffineTransformMakeTranslation(0, view.bounds.size.height);
    transform = CGAffineTransformScale(transform, 1.f, -1.f);
    CFIndex idx = -1;
    for (int i = 0; i < count; i++) {
        CGPoint linePoint = origins[i];
        CTLineRef line = CFArrayGetValueAtIndex(lines, i);
        // 获得每一行的CGRect信息
        CGRect flippedRect = [self getLineBounds:line point:linePoint];
        CGRect rect = CGRectApplyAffineTransform(flippedRect, transform);
        
        if (CGRectContainsPoint(rect, point)) {
            // 将点击的坐标转换成相对于当前行的坐标
            CGPoint relativePoint = CGPointMake(point.x-CGRectGetMinX(rect),
                                                point.y-CGRectGetMinY(rect));
            // 获得当前点击坐标对应的字符串偏移
            idx = CTLineGetStringIndexForPosition(line, relativePoint);
        }
    }
 
    // 释放内存
    CFRelease(frame);
    CFRelease(framesetter);
    return idx;
}
+ (CTFrameRef)createFrameWithFramesetter:(CTFramesetterRef)framesetter
                                  config:(CGFloat )config
                                  height:(CGFloat)height {
    ///Users/ios/Desktop/代码布局/代码布局/getLabelCharPoint.m:35:49: Incompatible pointer types passing retainable parameter of type '__strong id'to a CF function expecting 'CTFramesetterRef _Nonnull' (aka 'const struct __CTFramesetter *') type
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, NULL, CGRectMake(0, 0, config, height));
    
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
    CFRelease(path);
    return frame;
}
+ (CGRect)getLineBounds:(CTLineRef)line point:(CGPoint)point {
    CGFloat ascent = 0.0f;
    CGFloat descent = 0.0f;
    CGFloat leading = 0.0f;
    CGFloat width = (CGFloat)CTLineGetTypographicBounds(line, &ascent, &descent, &leading);
    CGFloat height = ascent + descent;
    return CGRectMake(point.x, point.y - descent, width, height);
}

@end
